﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace pardusshipcalc
{
    class Missiles
    {
        //http://forum.pardus.at/archive/index.php?showtopic=13388
        // Developed by Influence D, derived from Warbird's Pardus Bible formula by Baron using a Javascript Genetic Algorithm Engine
        // Ported to Visual C# by Richard Broker a.k.a Pigeon.
        // Error compared to manual: 0.025
        public double calcMissileHitChance(int missile_int, int ship_size, int evasion_bonus_eng, int evasion_bonus_ecm)
        {
            string dbg = "";
            try
            {
                var sqRSize = Math.Sqrt(ship_size);

                var base_val = 8 + (3.7215 * sqRSize);
                var step_val = 7.0481 + (2.8091 * sqRSize) + (-2.4899 * Math.Log10(ship_size));


                var v = (1 - evasion_bonus_eng) * ((missile_int * step_val) + base_val);
                if (evasion_bonus_ecm != 0)
                    v = (1 - evasion_bonus_ecm) * (v - Math.Max(0, (v - 100) / 2));
                return Math.Min(100, Math.Max(0, Math.Round(v)));
            }
            catch (Exception)
            {
                MessageBox.Show("calcMissileHitChance failed: " + dbg + "" + Environment.NewLine + "" + Environment.NewLine + "", "Alert");
            }
            return -1;
        }
    }
}
